// pages/settlement/settlement.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    jb_index: 0,
    lb_index: 0,
    num_type: 1,
    num: 0,
    type_arr: ['医院', '药房', '体检机构', '诊所'],
    content: "",
    regin: [],
    latitude: "",
    longitude: "",
    code:"",
    codes:"",
  },


  jbtab_chose: function (e) {
    var index = e.currentTarget.dataset.id
    this.setData({
      jb_index: index
    })
  },
  lbtab_chose: function (e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      lb_index: index
    })
  },
  phone_input: function (e) {
    var phone = e.detail.value
    this.setData({
      hospitaltel: phone
    })
  },
  uploadimgs(e) {
    var uniacid = app.siteInfo.uniacid;
    let t = this;
    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        let tempFilePaths = res.tempFilePaths[0];
        wx.uploadFile({
          url: t.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Uploadback&m=hyb_yl',
          filePath: tempFilePaths,
          name: 'upfile',
          formData: {},
          success: function (res) {
            t.setData({
              logo:res.data
            })
          },
          fail: function (res) {
            console.log(res)
          },
        })
      }
    })
  },
  cord_btn: function (e) {
    var that = this
    var phone = that.data.hospitaltel;
    var uniacid = app.siteInfo.uniacid
    var str = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
    if(phone == '')
    {
      wx.showToast({
        title: '请输入手机号',
      })
    }else if(!str.test(phone))
    {
      wx.showToast({
        title: '请正确输入手机号',
      })
    }else{
      app.util.request({
        'url':that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=SendSms&m=hyb_yl',
        data:{
          phoneNum:phone
        },
        success:function(res){
          that.setData({
            codes:res.data
          })
        }
      })
    }
    
    var num = 60
    var djstime = setInterval(function () {
      num--
      if (num <= 0) {
        that.setData({
          num_type: 1,
          num: 0
        })
        clearInterval(djstime)
        return
      } else {
        that.setData({
          num_type: 0,
          num: num
        })
      }

    }, 1000)
  },
  submit: function (e) {
    var that = this;
    var values = e.detail.value;
    values.openid = wx.getStorageSync('openid');
    values.hid = that.data.info.hid;
    values.address = that.data.city_text;
    values.xxaddress = that.data.xxaddress;
    values.logo = that.data.logo;
    values.groupid = that.data.lb_index + 1;
    values.grade = that.data.jb_index
    values.lntroduction = that.data.lntroduction;
    if(values.agentname == '')
    {
      wx.showToast({
        title: '请输入机构名称',
      })
    }else if(values.username == '')
    {
      wx.showToast({
        title: '请输入登录账号',
      })
    }else if(values.password == '')
    {
      wx.showToast({
        title: '请输入登录密码',
      })
    }else if(values.realname == '')
    {
      wx.showToast({
        title: '请输入负责人姓名',
      })
    }else if(values.hospitaltel == '')
    {
      wx.showToast({
        title: '请输入联系电话',
      })
    }else if(values.address == '')
    {
      wx.showToast({
        title: '请选择机构所在地',
      })
    }else if(values.xxaddress == '')
    {
      wx.showToast({
        title: '请输入详细地址',
      })
    }else if(values.logo == '')
    {
      wx.showToast({
        title: '请上传LOGO',
      })
    }else if(values.groupid == 0)
    {
      wx.showToast({
        title: '请选择机构类别',
      })
    }else if(values.grade == 0)
    {
      wx.showToast({
        title: '请选择机构级别',
      })
    }else if(that.data.codes != '' && values.code == '')
    {
      wx.showToast({
        title: '请输入验证码',
      })
    }else if(that.data.codes != values.code)
    {
      wx.showToast({
        title: '验证码不正确',
      })
    }else{
      app.util.request({
        'url':"entry/wxapp/regin.update_hospital",
        data:values,
        success:function(res){
          wx.showToast({
            title: '编辑成功',
          })
          setTimeout(() => {
            wx.navigateBack({ })
          }, 1000);
        }
      })
    }
  },
  city_chose: function (e) {
    console.log(e.detail.value.join('-'))
    this.setData({
      city_text: e.detail.value.join('-')
    })
  },

  mobel_show: function () {
    this.setData({
      rule_state: true
    })
  },
  close_zhe: function () {
    this.setData({
      rule_state: false
    })
  },
  tongyi: function (e) {
    this.setData({
      rule_state: false,
      check_state: true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.getAdv();
    that.getHospital();
    that.getLevel();
    that.getneworder();
    app.util.request({
      'url': 'entry/wxapp/hzbingli.url',
      success: function (res) {
        console.log(res)
        that.setData({
          url: res.data,
        })
      }
    })
  },
  changecontent:function(e){
    this.setData({
      lntroduction:e.detail.value
    })
  },
  // 获取机构详情
  getHospital: function () {
    var that = this;
    app.util.request({
      url: "entry/wxapp/regin.hospitals",
      data: {
        openid: wx.getStorageSync('openid')
      },
      success: function (res) {
        var lb_index = res.data.groupid-1;
        that.setData({
          info: res.data,
          lntroduction: res.data.lntroduction,
          latitude: res.data.latitude,
          longitude: res.data.longitude,
          content: res.data.content,
          xxaddress: res.data.xxaddress,
          lb_index: lb_index,
          jb_index: res.data.grade,
          logo:res.data.logo,
        })
        var address = res.data.address;
        if (address != '') {
          var addresss = address.split("-");
          that.setData({
            region: addresss,
            city_text:address
          })
        }

      }
    })
  },
  mapViewTap() {
    var that = this
    wx.authorize({
      scope: 'scope.userLocation',
      complete: function (res) {
        wx.chooseLocation({
          success(str) {
            var latitude = str.latitude
            var longitude = str.longitude
            var key = 'V7DBZ-K7C22-SXXUJ-CDUE7-AM2LH-AEFCM';
            //发送请求通过经纬度反查地址信息
            var getAddressUrl = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + str.latitude + "," + str.longitude + "&key=" + key + "&get_poi=1";
            wx.request({
              url: getAddressUrl,
              success: function (result) {
                
                var address = result.data.result.address
                console.log(address)
                that.setData({
                  xxaddress: address,
                  longitude: longitude,
                  latitude: latitude
                })
              }
            })
          }
        })
      }
    })
  },
  // 获取轮播
  getAdv: function () {
    var that = this;
    app.util.request({
      url: "entry/wxapp/index.lunbo",
      data: {
        position: 21
      },
      success: function (res) {
        that.setData({
          adv: res.data
        })
      }
    })
  },
  // 获取机构等级
  getLevel: function () {
    var that = this;
    app.util.request({
      'url': 'entry/wxapp/regin.getorganlevel',
      success: function (res) {
        that.setData({
          level_arr: res.data,
        })
      }
    })
  },
  // 获取最新用户数
  getneworder: function () {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/index.neworder",
      data: {
        type: 'index'
      },
      success: function (res) {
        that.setData({
          order: res.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})